package person.tavion;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.junit.jupiter.api.Test;
import person.tavion.service.ProvideService;

import java.time.LocalDateTime;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public class ProducerTest implements ProvideService {

    @Test
    @Override
    public void send() {
        // 创建Kafka生产者实例
        Producer<String, String> producer = new KafkaProducer<>(KafkaUtil.getProperties(TypeEnum.Producer));
        // 创建消息对象
        producer.send(new ProducerRecord<String, String>(KafkaUtil.topicName,"partition0",Integer.toString(1)));
        ProducerRecord<String, String> record = new ProducerRecord<>(KafkaUtil.topicName, "time", LocalDateTime.now()+"");
        // 发送消息
        Future<RecordMetadata> rs = producer.send(record);
//        try {
//            RecordMetadata rm = rs.get();
//            System.out.println(rm.toString());
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        } catch (ExecutionException e) {
//            throw new RuntimeException(e);
//        }
        System.out.println("发送成功");
        producer.close();

//        System.exit(0);
    }

    @Override
    public void delete() {

    }

    @Override
    public void list() {

    }
}
